from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest

from celery import Celery

app = Celery(
    'sms',  # 当前模块的名字
    broker='amqp://guest@localhost//',  # 消息队列的url
    # backend='amqp://guest@localhost//',  # 将调用的结果存储到MQ中
    backend='redis://:888888@localhost:6379/9'  # 将调用的结果存储到Redis中
)

@app.task
def send_sms(phone_numbers, code):
    client = AcsClient('LTAI4Fsft452XACNb3Xy7FMz', 'JfnnHUh5Q5GcxKRCLiO52QmZwR4ycD', 'cn-hangzhou')

    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', phone_numbers)
    request.add_query_param('SignName', "duoduo123")
    request.add_query_param('TemplateCode', "SMS_176521357")
    request.add_query_param('TemplateParam', "{\"code\":\"%s\"}" % code)

    response = client.do_action(request)
    # python2:  print(response)
    return str(response, encoding='utf-8')


# if __name__ == "__main__":
#     ret = send_sms('13285173126', '123456')
#     print(ret)


